<template>
  <div>
     <!-- 顶部导航 -->
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="保存"
      class=""
      @click-left="$emit('close')"
      @click-right="onConfirm"
    />
    <van-field
      center
      clearable
      maslength="12"
      show-word-limit
      placeholder="请输入新的昵称"
    />
  </div>
</template>

<script>
import { updateProfileAPI } from '@/api'
export default {
  name1: '',
  props: {
    value: {
      type: String,
      required:true
    }
  },
  data () {
    return {
      localName: this.value
    }
  },
  methods: {
    async onConfirm () {
      try {
        const localName = this.localName
        if (!localName.length) {
          this.$toast('昵称不能为空')
          return
        }
        await updateProfileAPI({
          name: '渠梦臣'
        })
        // 更新视图
        this.$emit('input', localName)
        // 关闭弹层
        this.$emit('close')
      } catch (error) {
        this.$toast.fail('更新失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.van-nav-bar{
  background-color: #a0ace3;
  .van-nav-bar__text {
    color: #93da77;
}
}
.van-field{
  padding: 15px;
}
</style>
